<template>
    <div class="wrapper" @click="cateShow = !cateShow">
        <filter-report :visible=true :cateShow="cateShow" :type="2"></filter-report>

        <el-table v-if="oneDay"  :data="followList" stripe border height="560">
            <el-table-column prop="deptName" label="部门/团队" width="100" fixed ></el-table-column>
            <el-table-column v-if="tableType == '0'" prop="userName" label="销售员"></el-table-column>
            <el-table-column prop="zero2eight" label="0-8点"></el-table-column>
            <el-table-column prop="eight2nine" label="8-9"></el-table-column>
            <el-table-column prop="nine2ten" label="9-10"></el-table-column>
            <el-table-column prop="ten2eleven" label="10-11"></el-table-column>
            <el-table-column prop="eleven2twelve" label="11-12"></el-table-column>
            <el-table-column prop="twelve2thirteen" label="12-13"></el-table-column>
            <el-table-column prop="thirteen2fourteen" label="13-14"></el-table-column>
            <el-table-column prop="fourteen2fifteen" label="14-15"></el-table-column>
            <el-table-column prop="fifteen2sixteen" label="15-16"></el-table-column>
            <el-table-column prop="sixteen2seventeen" label="16-17"></el-table-column>
            <el-table-column prop="seventeen2eighteen" label="17-18"></el-table-column>
            <el-table-column prop="eighteen2twentyThree" label="18-24"></el-table-column>
            <el-table-column prop="total" label="总计"></el-table-column>
        </el-table>
        
        <el-table v-else  :data="followList" stripe border height="560">
            <el-table-column prop="deptName" label="部门/团队" width="100" fixed ></el-table-column>
            <el-table-column v-if="tableType == '0'" prop="userName" label="销售员"></el-table-column>
            <el-table-column v-for="(item,index) in dataList"  :prop="item" :key="index" :label="item"></el-table-column>
            <el-table-column prop="allCount" label="总计"></el-table-column>
        </el-table>

    </div>
</template>

<script>
    import Vue from 'vue'
    import {mapGetters} from 'vuex'
    import FilterReport from '~components/filterReport.vue'
    import { getTime,getDate,getYesterday,getSevenDaysBefore } from '~/utils/auth'
    import { Radio, RadioGroup, RadioButton, Table, TableColumn ,Message} from 'element-ui'
    Vue.use(Radio);
    Vue.use(RadioGroup);
    Vue.use(RadioButton);
    Vue.use(Table);
    Vue.use(TableColumn);

    export default {
        components: {
            FilterReport
        },
        head(){
            return {
                title: "跟进量统计",
                meta: [
                    { hid: 'followList', name: 'followList', content: 'followList'}
                ]
            }
        },
        data () {
            return {
                tabName: '跟进量统计',
                cateShow:false,
                tableType:'1',
                userName: "",
                oneDay:false,
                dataList:[],
                filterDept : {
                    time:  getYesterday(),
                    deptId: '1'
                },
                filterPer : {
                    startTime: getYesterday(),
                    endTime: getYesterday(),
                    deptId: '1'
                },
                followList:[],
                filter : {
                    startDate:"",
                    endDate:"",
                    customerlevel: '',//客户类型
                    deptId: '',
                    usId:'',
                    contactState:"",
                    flag:'',
                    level:''
                }
            }
        },
        computed: {
            ...mapGetters({
                'pageTabsList': 'getPageTabsList'
            })
        },
        methods: {
            getFilterData(data){
                //this.filter.startDate = data.dateRange[0]?getDate(data.dateRange[0]):getSevenDaysBefore();
                //this.filter.endDate =  data.dateRange[1]?getDate(data.dateRange[1]):getYesterday(new Date());
                this.filter.startDate = data.dateRange[0]?getDate(data.dateRange[0]):getDate(new Date());
                this.filter.endDate =  data.dateRange[1]?getDate(data.dateRange[1]):getDate(new Date());
                this.oneDay = (this.filter.startDate === this.filter.endDate)?true:false;
                this.filter.customerlevel = data.customerlevel || '337,338,339,340,341,342,343,344,345';
                this.filter.contactState = data.contactState;
                this.filter.deptId = data.deptId;
                this.filter.usId = data.usId;
                this.userName = data.userName;
                this.filter.flag = data.flag;
                this.filter.level = data.level;

                if(this.filter.flag == 2 || this.filter.flag == 1){
                    this.tableType = "0"//团队
                }else if(this.filter.flag == 0){
                    this.tableType = "1"//部门
                }

                // if(this.filter.level == 5 || this.filter.usId){
                //     this.tableType = "0"//团队
                // }else{
                //     this.tableType = "1"//部门
                // }
                this.getFollowList(this.filter);
            },
            getFollowList(filter) {
                this.$store.dispatch('getFollowList',filter)
                    .then((response) => {
                        let result = response.data;
                        this.followList =[];
                        this.dataList = [];
                        if (result && result.data && result.data.list || result.data.followHoursList) {
                            let dataList = result.data.list;
                            let dataHoursList = result.data.followHoursList;
                            this.tableType = result.data.isNormalUser?"0":"1"//如果部门下只有人，怎按团队展示
                            if(this.oneDay && dataHoursList.length>0){//如果是同一天
                                let deptAll = {
                                        deptId:null,
                                        deptName:"",
                                        userName:"",
                                        zero2eight:0,
                                        eight2nine:0,
                                        nine2ten:0,
                                        ten2eleven:0,
                                        eleven2twelve:0,
                                        twelve2thirteen:0,
                                        thirteen2fourteen:0,
                                        fourteen2fifteen:0,
                                        fifteen2sixteen:0,
                                        sixteen2seventeen:0,
                                        seventeen2eighteen:0,
                                        eighteen2twentyThree:0,
                                        recordAll:null,
                                        total:null,
                                        userId:null
                                    }
                                for(var i=0; i <dataHoursList.length; i++) {
                                    deptAll.zero2eight += dataHoursList[i].zero2eight;
                                    deptAll.eight2nine += dataHoursList[i].eight2nine;
                                    deptAll.nine2ten += dataHoursList[i].nine2ten;
                                    deptAll.ten2eleven += dataHoursList[i].ten2eleven;
                                    deptAll.eleven2twelve += dataHoursList[i].eleven2twelve;
                                    deptAll.twelve2thirteen += dataHoursList[i].twelve2thirteen;
                                    deptAll.thirteen2fourteen += dataHoursList[i].thirteen2fourteen;
                                    deptAll.fourteen2fifteen += dataHoursList[i].fourteen2fifteen;
                                    deptAll.fifteen2sixteen += dataHoursList[i].fifteen2sixteen;
                                    deptAll.sixteen2seventeen += dataHoursList[i].sixteen2seventeen;
                                    deptAll.seventeen2eighteen += dataHoursList[i].seventeen2eighteen;
                                    deptAll.eighteen2twentyThree += dataHoursList[i].eighteen2twentyThree;
                                    //deptAll.total += dataHoursList[i].total;
                                }

                                dataHoursList.forEach((n,i)=>{
                                    dataHoursList[i].userName = dataHoursList[i].userName || this.userName;
                                })
                                deptAll.deptName = this.tableType == '1' ? "总计":"";
                                deptAll.userName = this.tableType == '0' ? "总计":"";
                                this.followList = dataHoursList;
                                this.followList.push(deptAll);
                            }else if(dataList.length>0){//如果不是同一天
                                let followDataList = [];

                                let deptAll = {
                                    deptName:'',
                                    userName:''
                                };
                                deptAll.deptName = this.tableType == '1' ? "总计":"";
                                deptAll.userName = this.tableType == '0' ? "总计":"";

                                for(let n = 0; n <dataList.length; n++){
                                    let inList = dataList[n];
                                    let option = {}
                                    let allCount = 0;
                                    for(let j = 0; j<inList.length; j++){
                                        let day = (j===0)?inList[j].day:inList[j].day.substring(5,10);
                                        option[day] = inList[j].total;
                                        allCount+=inList[j].total;

                                        deptAll[day] = (deptAll[day] || 0)+inList[j].total;
                                        //deptAll.allCount = (deptAll.allCount || 0)+inList[j].total


                                        //表头
                                        if(n === 0){
                                            this.dataList.push(day);
                                        }
                                    }
                                    option.deptName = inList[0].deptName;
                                    option.userName = inList[0].userName || this.userName;
                                    option.allCount = allCount;
                                    followDataList.push(option)
                                }
                                followDataList.push(deptAll)

                                this.followList = followDataList
                            }
                        }
                });
            }
        },
        created () {
            //pageTab
            if(process.BROWSER_BUILD) {
                if(this.pageTabsList[this.pageTabsList.length-1].name == 'CRM') {
                    let arr = this.pageTabsList;
                    arr[arr.length-1].name = this.tabName;
                    this.$store.commit('PAGETABSLIST',{data:arr})
                }
            }
        }
    }

</script>

<style scoped lang="scss">
    .wrapper{
        min-height: 500px;
    }
    .button-group {
        position: relative;
        margin: 30px 0;
    }
</style>